package org.eden.rpc.core.codec;

import org.eden.rpc.common.enumerate.SerializerCode;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public interface Serializer {

    Map<Integer, Serializer> serializerInstances = new ConcurrentHashMap<>();

    byte[] serialize(Object data);

    <T> T deserialize(byte[] bytes, Class<T> clazz);

    int getCode();

    static Serializer getInstance(int serializerCode) {
        if (!serializerInstances.containsKey(serializerCode)) {
            if (serializerCode == SerializerCode.KRYO.getType()) {
                serializerInstances.put(serializerCode, new KryoSerializer());
            }
        }
        // 不存在则返回null
        return serializerInstances.get(serializerCode);
    }
}
